using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.AcmsControls
{
    public class TemplateLoaderControl : WebControl, INamingContainer
    {
        protected override void  OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!Page.IsPostBack || (Page.PreviousPage != null && Page.PreviousPage.IsCrossPagePostBack))
            {
                Page.Title = string.Format("{0} {1}", Post.SiteName, Post.DisplayName);
            }
        }

        private int postId;
        public int PostId
        {
            get { return postId; }
            set { postId = value; }
        }

        private string pageTemplateUrl;
        public string PageTemplateUrl
        {
            get 
            {
                return pageTemplateUrl; 
            }
            set
            {
                pageTemplateUrl = value;
            }
        }

        private Acms.Bll.Post post;
        protected Acms.Bll.Post Post
        {
            get
            {
                if (post == null && PostId > 0)
                {
                    post = Acms.Bll.Post.GetById(postId);
                }
                if (post == null)
                {
                    throw new Acms.Exceptions.PostNotFoundException(string.Format("Post '{0}' could not be loaded.", postId.ToString()));
                }
                return post;
            }
        }

        protected override object SaveViewState()
        {
            ViewState["PageTitle"] = Page.Title;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if (ViewState["PostId"] is int)
            {
                postId = (int)ViewState["PostId"];
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            Control ctrl = Page.LoadControl(PageTemplateUrl);
            if (ctrl != null)
            {
                if (ctrl is BaseTemplateControl)
                {
                    ((BaseTemplateControl)ctrl).CurrentPostRequired += new EventHandler(TemplateLoaderControl_CurrentPostRequired);
                    this.Controls.Add(ctrl);
                }
                else
                {
                    this.Controls.Add(ctrl);
                    //throw new ApplicationException(string.Format("'{0}' does not inherit from BaseTemplateControl", PageTemplateUrl));
                }
            }
            else
            {
                throw new ApplicationException(string.Format("'{0}' could not be found", PageTemplateUrl));
            }
        }

        private void TemplateLoaderControl_CurrentPostRequired(object sender, EventArgs e)
        {
            CurrentTemplateControl.CurrentPost = Post;
        }

        private BaseTemplateControl CurrentTemplateControl
        {
            get 
            {
                EnsureChildControls();
                return this.Controls[0] as BaseTemplateControl;
            }
        }

        public override void DataBind()
        {
            base.DataBind();
            Page.Title = string.Format("{0} {1}", Post.SiteName, Post.DisplayName);
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
        }
    }
}
